home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / DESKLIBC / SOURCES.ZIP / DeskLib / !DLSources / Libraries / Event / c / MClaimInit < prev    next >
Text File  |  1995-07-08  |  4KB  |  147 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Event.MClaimInit.c
  12.     Author:  Copyright © 1992, 1993, 1994 Jason Williams
  13.     Version: 1.04 (22 Oct 1994)
  14.     Purpose: Extension to Event.c to allow routing of specific message types
  15.              to different windows' message handlers.
  16. */
  17.  
  18.  
  19. #include "EMsgDefs.h"
  20.  
  21. #define ERRBASE 1
  22. #define ERR1 ERRBASE+0
  23. #define ERRMESS1 "Unable to allocate memory for eventmsg claim"
  24.  
  25. linklist_header eventmsg__claimanchor = {NULL, NULL};
  26.  
  27.  
  28. static BOOL EventMsg_DispatchMessage(event_pollblock *event, void *reference)
  29. {
  30.   message_action action;
  31.   window_handle  window;
  32.   eventmsg_claimrecord *ptr, *nextptr;
  33.   eventmsg_windowrecord *wptr, *nextwptr;
  34.  
  35.   UNUSED( reference);
  36.   
  37.   action = event->data.message.header.action;
  38.   switch(action)
  39.   {
  40.     case message_DATASAVE:
  41.     case message_DATASAVEACK:
  42.     case message_DATALOAD:
  43.       window = event->data.message.data.words[0];
  44.       break;
  45.  
  46.     case message_HELPREQUEST:
  47.       window = event->data.message.data.helprequest.where.window;
  48.       break;
  49.  
  50.     default:
  51.       window = event_ANY;
  52.       break;
  53.   }
  54.  
  55.   ptr = (eventmsg_claimrecord *) eventmsg__claimanchor.next;
  56.   while (ptr != NULL)
  57.   {
  58.     nextptr = (eventmsg_claimrecord *) ptr->header.next;
  59.  
  60.     if (ptr->messagetype == event_ANY || ptr->messagetype == action)
  61.     {
  62.       wptr = (eventmsg_windowrecord *) ptr->windowlist.next;
  63.       while (wptr != NULL)
  64.       {
  65.         nextwptr = (eventmsg_windowrecord *) wptr->header.next;
  66.  
  67.         if (window == event_ANY || wptr->window == event_ANY ||
  68.             wptr->window == window)
  69.           if (wptr->handler(event, wptr->reference) == TRUE)
  70.             return(TRUE);
  71.  
  72.         wptr = nextwptr;
  73.       }
  74.     }
  75.     ptr = nextptr;
  76.   }
  77.   return(FALSE);
  78. }
  79.  
  80.  
  81.  
  82. extern BOOL EventMsg_Claim(message_action messagetype, window_handle window,
  83.                            event_handler handler, void *reference)
  84. {
  85.   eventmsg_claimrecord *ptr;
  86.   eventmsg_windowrecord *wrecord;
  87.  
  88.   ptr = (eventmsg_claimrecord *) eventmsg__claimanchor.next;
  89.   while (ptr != NULL)            /* Find claims for this message action type */
  90.   {
  91.     if (ptr->messagetype == messagetype)
  92.       break;
  93.  
  94.     ptr = (eventmsg_claimrecord *) ptr->header.next;
  95.   }
  96.  
  97.   if (ptr == NULL)                /* No current claims, so add new claimlist */
  98.   {
  99.     ptr = (eventmsg_claimrecord *) malloc(sizeof(eventmsg_claimrecord));
  100.     if (ptr == NULL)
  101.     {
  102.       Error_ReportInternal(ERR1, ERRMESS1);
  103.       return(FALSE);
  104.     }
  105.  
  106.     LinkList_Init(&(ptr->header));
  107.     ptr->messagetype = messagetype;
  108.     LinkList_Init(&(ptr->windowlist));
  109.     LinkList_AddToHead(&eventmsg__claimanchor, &(ptr->header));
  110.                                           /* Insert new message_handler list */
  111.   }
  112.  
  113.   wrecord = (eventmsg_windowrecord *) malloc(sizeof(eventmsg_windowrecord));
  114.   if (wrecord == NULL)
  115.   {
  116.     Error_ReportInternal(ERR1, ERRMESS1);
  117.     return(FALSE);
  118.   }
  119.  
  120.   LinkList_Init(&(wrecord)->header);
  121.   wrecord->window    = window;
  122.   wrecord->handler   = handler;
  123.   wrecord->reference = reference;
  124.  
  125.  /* Insert new window's handler into the window list for this message action.
  126.   * Specific-window handlers are placed on the front of the list, and non-
  127.   * specific handlers are placed on the end, to give priority to the most
  128.   * specific claim on any message.
  129.   */
  130.   if (window == event_ANY)            /* insert new window's handler in list */
  131.     LinkList_AddToTail(&(ptr->windowlist), &(wrecord->header));
  132.   else
  133.     LinkList_AddToHead(&(ptr->windowlist), &(wrecord->header));
  134.  
  135.   return(TRUE);
  136. }
  137.  
  138.  
  139.  
  140. extern void EventMsg_Initialise(void)
  141. {
  142.   Event_Claim(event_USERMESSAGE, event_ANY, event_ANY,
  143.               EventMsg_DispatchMessage, NULL);
  144.   Event_Claim(event_USERMESSAGERECORDED, event_ANY, event_ANY,
  145.               EventMsg_DispatchMessage, NULL);
  146. }
  147.